package com.csst.business.action;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.id.UUIDGenerator;

import com.csst.business.model.Award;
import com.csst.business.model.AwardResult;
import com.csst.business.model.Awresult;
import com.csst.business.model.TWuser;
import com.csst.business.service.IAwardService;
import com.csst.business.service.IUserService;
import com.csst.business.util.PageData;
import com.csst.business.util.PaginationUtil;
import com.csst.popular.api.SnsAPI;
import com.csst.popular.bean.SnsToken;
import com.csst.popular.bean.User;
import com.csst.services.Thread.TokenThread;
import com.opensymphony.xwork2.ActionSupport;


/**
 * 抽奖概率算法来自：http://www.helloweba.com/view-blog-216.html
 * @author handq
 *
 */
@Entity
@SuppressWarnings("serial")
public class AwardAction extends ActionSupport {
	private IAwardService awardService;
	private Award award;
	private AwardResult aresult;
	private List<Awresult> resultList;
	private Awresult awresult;
	private IUserService userService;
	//分页参数
	private PageData pageData;
	private int pagesize=10;
	private  int currentPage = 1;
	
	Object prizeArr[][] = new  Object[][]{
			//id,min,max，prize【奖项】,v【中奖率】
			//外面的转盘转动【微信里看起来卡顿卡顿的】
//			{1,1,14,"一等奖",1},
//			{2,346,364,"一等奖",1},
//			{3,16,44,"不要灰心",10},
//			{4,46,74,"神马也没有",10},
//			{5,76,104,"祝您好运",10},
//			{6,106,134,"二等奖",2},
//			{7,136,164,"再接再厉",10},
//			{8,166,194,"神马也没有",10},
//			{9,196,224,"运气先攒着",10},
//			{10,226,254,"三等奖",5},
//			{11,256,284,"要加油哦",10},
//			{12,286,314,"神马也没有",10},
//			{13,316,344,"谢谢参与",10}	
			//里面的指针转动
			{1,1,14,"一等奖",1},
			{2,346,364,"一等奖",1},
			{3,16,44,"不要灰心",10},
			{4,46,74,"神马也没有",10},
			{5,76,104,"祝您好运",10},
			{6,106,134,"二等奖",2},
			{7,136,164,"再接再厉",10},
			{8,166,194,"神马也没有",10},
			{9,196,224,"运气先攒着",10},
			{10,226,254,"三等奖",3},
			{11,256,284,"要加油哦",10},
			{12,286,314,"神马也没有",10},
			{13,316,344,"谢谢参与",10}
	};
	
	
	
	/**
	 * 抽奖的业务逻辑
	 * @return
	 */		
	public String getResult(){
		HttpServletRequest request = ServletActionContext.getRequest();
		User user = (User) request.getSession().getAttribute("userinfo");
		aresult = new AwardResult();
		if(user!=null){
			int count = awardService.getAwardCount(user.getOpenid());
			if(count >=3){
				aresult.setSuccess(false);
				aresult.setPrize("1000");
				aresult.setMsg("对不起，您今天的三次机会都已经用完了，明天再来吧！");
			}else{
				Object obj[] = choujiang();//抽奖后返回角度和奖品等级
				award = new Award();
				award.setAddTime(new Date());
				award.setUserOpenId(user.getOpenid());
				award.setPrizetype(String.valueOf(obj[2]));
				award.setAtype("award");
				awardService.saveAwardData(award);
				aresult.setSuccess(true);
				aresult.setAngle(String.valueOf(obj[0]));
				aresult.setPrize(String.valueOf(obj[1]));
				aresult.setMsg(String.valueOf(obj[2]));
				System.out.println(String.valueOf(obj[0])+"-->"+String.valueOf(obj[1])+"-->"+String.valueOf(obj[2]));
			}
			
		}else{
			aresult.setSuccess(false);
			aresult.setMsg("只有我们的微信绑定会员才可以抽奖哟！您可以在微信里注册会员并绑定.");
		}
		return SUCCESS;
	}
	
	public boolean saveResultAjax(String prized,String scno,String phone){
		HttpServletRequest request = ServletActionContext.getRequest();
		User user = (User) request.getSession().getAttribute("userinfo");
		if(user!=null){
			Awresult awresult = new Awresult();
			awresult.setAddTime(new Date());
			awresult.setPhone(phone);
			awresult.setPrized(prized);
			awresult.setScno(scno);
			awresult.setUserName(user.getNickname());
			awresult.setUserOpenId(user.getOpenid());
			awresult.setPtype("大转盘");
			awresult.setPstate("未兑奖"); 
			awardService.saveAwresultData(awresult);
			return true;
		}else{
			return false;
		}
	}
	
	public String getReList(){
		int totle = awardService.getResultTotle();
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		resultList =  awardService.getAllResultList(pageData.getStartNum(),pageData.getEndNum());
		return "resultlist";
	}
	
	public String getAwresultByOpenId(){
		int totle = awardService.getResultByOpIdTotle(awresult.getUserOpenId());
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		resultList =  awardService.getAllResultList(awresult.getUserOpenId(),pageData.getStartNum(),pageData.getEndNum());
		return "memberResultlist";
	}
	
	public String getWAwresultByOpenId(){
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser) request.getSession().getAttribute("tuser");
		if(tuser!=null){
			int totle = awardService.getResultByOpIdTotle(awresult.getUserOpenId());
			pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
			resultList =  awardService.getAllResultList(awresult.getUserOpenId(),pageData.getStartNum(),pageData.getEndNum());
			return "WmemberResultlist";
		}else{
			return "error";
		}
		
	}

	public boolean updateResultAjax(String id){
		awardService.updateResult(id);
		return true;
	}
	
	//抽奖并返回角度和奖项
	public Object[] choujiang(){
		//概率数组
		Integer obj[] = new Integer[prizeArr.length];
		for(int i=0;i<prizeArr.length;i++){
			obj[i] = (Integer) prizeArr[i][4];
		}
		Integer prizeId = getRand(obj); //根据概率获取奖项id
		//旋转角度
		int angle = new Random().nextInt((Integer)prizeArr[prizeId][2]-(Integer)prizeArr[prizeId][1])+(Integer)prizeArr[prizeId][1];
		String msg = (String) prizeArr[prizeId][3];//提示信息
		return new Object[]{angle,prizeId,msg};
	}
	
	//根据概率获取奖项
	public Integer getRand(Integer obj[]){
		Integer result = null;
		try {
			int  sum = 0;//概率数组的总概率精度 
			for(int i=0;i<obj.length;i++){
				sum+=obj[i];
			}
			for(int i=0;i<obj.length;i++){//概率数组循环 
				int randomNum = new Random().nextInt(sum);//随机生成1到sum的整数
				if(randomNum<obj[i]){//中奖
					result = i;
					break;
				}else{
					sum -=obj[i];
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public IAwardService getAwardService() {
		return awardService;
	}
	public void setAwardService(IAwardService awardService) {
		this.awardService = awardService;
	}
	public Award getAward() {
		return award;
	}
	public void setAward(Award award) {
		this.award = award;
	}
	public AwardResult getAresult() {
		return aresult;
	}
	public void setAresult(AwardResult aresult) {
		this.aresult = aresult;
	}
	public Object[][] getPrizeArr() {
		return prizeArr;
	}
	public void setPrizeArr(Object[][] prizeArr) {
		this.prizeArr = prizeArr;
	}

	public List<Awresult> getResultList() {
		return resultList;
	}

	public void setResultList(List<Awresult> resultList) {
		this.resultList = resultList;
	}

	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public Awresult getAwresult() {
		return awresult;
	}

	public void setAwresult(Awresult awresult) {
		this.awresult = awresult;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	//getter&&setter
	
}
